package com.hp.attendance.serlvet.history.InService;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.History;
import com.hp.attendance.service.EmployeeBysService;
import com.hp.attendance.service.HistoryBysService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addEmployee")
public class EmployeeAddServlet extends HttpServlet {
    private EmployeeBysService employeeBysService = new EmployeeBysService();
    private HistoryBysService historyBysService = new HistoryBysService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加功能
        try {
            //获取填写的信息
            String name = req.getParameter("name");
            String gender = req.getParameter("gender");
            String birthday = req.getParameter("birthday");
            String employee_number = req.getParameter("employee_number");
            String in_time = req.getParameter("in_time");
            String telephone = req.getParameter("telephone");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String education = req.getParameter("education");
            String department_number = req.getParameter("department_number");
            String position_number = req.getParameter("position_number");
            String password2 = req.getParameter("password2");
            String notes = req.getParameter("notes");

            //字符串格式转换成日期格式
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date birth = format.parse(birthday.trim());
            Date in_time1 = format.parse(in_time.trim());

            //添加至employee对象
            Employee employee = new Employee(null,Integer.parseInt(employee_number) ,name,gender,birth,telephone,email,address,
                    null,education,Integer.parseInt(department_number.trim()),Integer.parseInt(position_number.trim()),
                    in_time1,password2,notes);
            employeeBysService.AddEmployee(employee);

            //添加至history对象   状态字段默认在职
            History history = new History(null, Integer.parseInt(employee_number), name, gender, birth, telephone, email, address,
                    null, education, in_time1, null, Integer.parseInt(department_number.trim()), Integer.parseInt(position_number.trim()),
                    "在职", null, notes);
            historyBysService.AddHistory(history);




            //跳转
            req.getRequestDispatcher("EmployeeList").forward(req,resp);
        }

        catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
